package com.anolesoft.yr.mbus.base.controller;

import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.base.dto.BaseAddressBookDTO;
import com.anolesoft.yr.mbus.base.service.BaseAddressBookService;
import com.anolesoft.yr.mbus.base.vo.BaseAddressBookVO;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.Constants;
import com.anolesoft.yr.util.PageInfo;
import com.anolesoft.yr.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/9/13 9:33
 */
@RestController
@RequestMapping("/api/base/addressbook")
public class BaseAddressBookController {
    @Autowired
    private BaseAddressBookService baseAddressBookService;

    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(BaseAddressBookVO baseAddressBookVO) {
        BaseAddressBookDTO baseAddressBookDTO = vo2dto(baseAddressBookVO, true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, baseAddressBookService.create(baseAddressBookDTO));
    }

    private BaseAddressBookDTO vo2dto(BaseAddressBookVO baseAddressBookVO, boolean newId) {
        return BaseAddressBookDTO.builder()
                .dept(baseAddressBookVO.getDept())
                .email(baseAddressBookVO.getEmail())
                .factoryCode(SecurityUtils.getFactoryCode())
                .name(baseAddressBookVO.getName())
                .phone(baseAddressBookVO.getPhone())
                .post(baseAddressBookVO.getPost())
                .remark(baseAddressBookVO.getRemark())
                .targetUid(baseAddressBookVO.getTargetUid())
                .type(baseAddressBookVO.getType())
                .objuid(newId ? CommonUtils.fastUUID() : baseAddressBookVO.getObjuid())
                .modifyUserUid(SecurityUtils.getUserId())
                .createUserUid(SecurityUtils.getUserId())
                .build();
    }

    @PutMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(BaseAddressBookVO baseAddressBookVO) {
        BaseAddressBookDTO baseAddressBookDTO = vo2dto(baseAddressBookVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, baseAddressBookService.modify(baseAddressBookDTO));
    }

    @DeleteMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, baseAddressBookService.remove(id));
    }
    @DeleteMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@RequestParam("ids[]") List<String> ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, baseAddressBookService.removeBatch(ids));
    }

    @GetMapping(value = "/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<BaseAddressBookDTO> baseAddressBookDTO = this.baseAddressBookService.selectOneById(id);
        if (baseAddressBookDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, dto2vo(baseAddressBookDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }
    }
    @GetMapping(value = "list/targetuid", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(@RequestParam("targetUid") String targetUid) {
        List<BaseAddressBookVO> collect = this.baseAddressBookService.selectListByTargetUid(targetUid).stream().map(t -> dto2vo(t)).collect(Collectors.toList());
        PageInfo<List<BaseAddressBookVO>> pageInfo = new PageInfo<>(collect.size(), collect);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    private BaseAddressBookVO dto2vo(BaseAddressBookDTO baseAddressBookDTO) {
        return BaseAddressBookVO.builder()
                .createTime(baseAddressBookDTO.getCreateTime())
                .createUserUid(baseAddressBookDTO.getCreateUserUid())
                .deleteFlag(baseAddressBookDTO.getDeleteFlag())
                .dept(baseAddressBookDTO.getDept())
                .email(baseAddressBookDTO.getEmail())
                .factoryCode(baseAddressBookDTO.getFactoryCode())
                .modifyTime(baseAddressBookDTO.getModifyTime())
                .modifyUserUid(baseAddressBookDTO.getModifyUserUid())
                .name(baseAddressBookDTO.getName())
                .objuid(baseAddressBookDTO.getObjuid())
                .phone(baseAddressBookDTO.getPhone())
                .post(baseAddressBookDTO.getPost())
                .remark(baseAddressBookDTO.getRemark())
                .targetUid(baseAddressBookDTO.getTargetUid())
                .type(baseAddressBookDTO.getType())
                .build();
    }
}
